package com.wc.算法基础课.D第四讲数学知识.矩阵乘法.牛站;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/5 14:37
 * @description https://www.acwing.com/problem/content/description/347/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 210;
    static int[][] g = new int[N][N];
    static Map<Integer, Integer> map = new HashMap<>();
    static int INF = 0x3f3f3f3f;
    static int id = 0;
    static int n, t, S, E;

    // 思路：d[k,i,j] 表示i ~ j 走了k步的最短路径长度
    public static void main(String[] args) {
        for (int i = 1; i < N; i++) Arrays.fill(g[i], INF);
        n = sc.nextInt();
        t = sc.nextInt();
        S = sc.nextInt();
        E = sc.nextInt();
        if (!map.containsKey(S)) map.put(S, ++id);
        if (!map.containsKey(E)) map.put(E, ++id);
        S = map.get(S);
        E = map.get(E);
        while (t-- > 0) {
            int a, b, c;
            c = sc.nextInt();
            a = sc.nextInt();
            b = sc.nextInt();
            if (!map.containsKey(a)) map.put(a, ++id);
            if (!map.containsKey(b)) map.put(b, ++id);
            a = map.get(a);
            b = map.get(b);
            g[a][b] = g[b][a] = Math.min(g[a][b], c);
        }
        int[][] res = qkm(n);
        out.println(res[S][E]);
        out.flush();
    }

    static int[][] qkm(int b) {
        int[][] res = new int[id + 1][id + 1];
        for (int i = 1; i <= id; i++) Arrays.fill(res[i], 1, id + 1, INF);
        for (int i = 1; i <= id; i++) res[i][i] = 0;
        while (b > 0) {
            if ((b & 1) == 1) mulMatrix(res, res, g);
            mulMatrix(g, g, g);
            b >>= 1;
        }
        return res;
    }

    static int[][] tmp = new int[N][N];

    static void mulMatrix(int[][] c, int[][] a, int[][] b) {
        for (int i = 1; i <= id; i++) {
            Arrays.fill(tmp[i], 1, id + 1, INF);
        }
        for (int k = 1; k <= id; k++) {
            for (int i = 1; i <= id; i++) {
                for (int j = 1; j <= id; j++) {
                    tmp[i][j] = Math.min(tmp[i][j], a[i][k] + b[k][j]);
                }
            }
        }
        for (int i = 1; i <= id; i++) {
            for (int j = 1; j <= id; j++) {
                c[i][j] = tmp[i][j];
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
